package com.gy.paidao.util;

import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Environment;

import com.leju.common.util.Logger;

import java.io.File;
import java.io.IOException;

/**
 * Created by 1071568866 on 2016/1/4.
 */
public class StorageUtils {

    private static final String EXTERNAL_STORAGE_PERMISSION = "android.permission.WRITE_EXTERNAL_STORAGE";

    private StorageUtils() {}

    public static File getNormaoDirectory(Context context){
        //"/storage/emulated/0/Android/data/com.example.mapdemo/files/mounted/qupaiVideo/"
        File appCacheDir = new File("/storage/emulated/0/Android/data/com.gy.paidao/files/mounted/qupaiVideo/");
        if(!appCacheDir.exists()){
            appCacheDir.mkdirs();
        }
        return appCacheDir;
    }

    /**
     * 语音存储路径优先SD卡存储
     * @param context
     * @return
     */
    public static File getCacheVoiceFileDirectory(Context context){
        return getCacheVoiceFileDirectory(context, true);
    }

    /**
     * 获取语音保存目录
     * @param context
     * @param preferExternal 是否优先外部sd卡存储
     * @return
     */
    public static File getCacheVoiceFileDirectory(Context context, boolean preferExternal){
        File appCacheDir = null;

        if (preferExternal){
            appCacheDir = getExternalVoiceFileDir(context);
        }
        if (appCacheDir == null) {
            appCacheDir = context.getCacheDir();
        }
        if (appCacheDir == null) {
            String cacheDirPath = "/data/data/" + context.getPackageName() + "/files/voice/";
            Logger.e("Can't define system cache directory! '%s' will be used.");
            appCacheDir = new File(cacheDirPath);
        }
        return appCacheDir;
    }

    /**
     * 获取图片目录结构优先SD卡
     * @param context
     * @return
     */
    public static File getCacheImageFileDirectory(Context context){
        return getCacheImageFileDirectory(context,true);
    }

    /**
     * 获取图片目录结构优先SD卡
     * @param context
     * @param preferExternal 是否优先外部SD开存储
     * @return
     */
    public static File getCacheImageFileDirectory(Context context,boolean preferExternal){
        File appCacheDir = null;
        if (preferExternal){
            appCacheDir = getExternalImageFileDir(context);
        }
        if (appCacheDir == null) {
            appCacheDir = context.getCacheDir();
        }
        if (appCacheDir == null) {
            String cacheDirPath = "/data/data/" + context.getPackageName() + "/files/image/";
            Logger.e("Can't define system cache directory! '%s' will be used.");
            appCacheDir = new File(cacheDirPath);
        }
        return appCacheDir;
    }

    /**
     * 目录结构：/storage/emulated/../Android/data/package_name/files/
     * @param context
     * @return
     */
    private static File getExternalCacheFileDir(Context context,boolean nomedia){
        File externalCacheDir = null;
        String externalStorageState;
        try {
            externalStorageState = Environment.getExternalStorageState();
        }catch (NullPointerException e){
            externalStorageState = "";
        }catch (IncompatibleClassChangeError e){
            externalStorageState = "";
        }
        if (Environment.MEDIA_MOUNTED.equals(externalStorageState) && hasExternalStoragePermission(context)){
            File dataDir = new File(new File(Environment.getExternalStorageDirectory(), "Android"), "data");
            externalCacheDir = new File(new File(dataDir, context.getPackageName()), "files");
            if (!externalCacheDir.exists()) {
                if (!externalCacheDir.mkdirs()) {
                    Logger.w("Unable to create external cache directory");
                    return null;
                }
                if (nomedia){
                    try {
                        new File(externalCacheDir, ".nomedia").createNewFile();
                    } catch (IOException e) {
                        Logger.i("Can't create \".nomedia\" file in application external cache directory");
                    }
                }
            }
        }
        return externalCacheDir;
    }

    /**
     * 目录结构：/storage/emulated/../Android/data/package_name/files/voice/
     * @param context
     * @return
     */
    private static File getExternalVoiceFileDir(Context context){
        File externalCacheFileDir = getExternalCacheFileDir(context,true);
        if (externalCacheFileDir != null){
            File voiceFileDir = new File(externalCacheFileDir,"voice");
            if (!voiceFileDir.exists()) {
                if (!voiceFileDir.mkdirs()) {
                    Logger.w("Unable to create external cache directory");
                    return null;
                }
                try {
                    new File(voiceFileDir, ".nomedia").createNewFile();
                } catch (IOException e) {
                    Logger.i("Can't create \".nomedia\" file in application external cache directory");
                }
            }
            return voiceFileDir;
        }
        return externalCacheFileDir;
    }

    /**
     * /storage/emulated/../Android/data/package_name/files/image/
     * @param context
     * @return
     */
    private static File getExternalImageFileDir(Context context){
        File externalCacheFileDir = getExternalCacheFileDir(context,true);
        if (externalCacheFileDir != null){
            File imageFileDir = new File(externalCacheFileDir,"image");
            if (!imageFileDir.exists()) {
                if (!imageFileDir.mkdirs()) {
                    Logger.w("Unable to create external cache directory");
                    return null;
                }
                try {
                    new File(imageFileDir, ".nomedia").createNewFile();
                } catch (IOException e) {
                    Logger.i("Can't create \".nomedia\" file in application external cache directory");
                }
            }
            return imageFileDir;
        }
        return externalCacheFileDir;
    }

    public static File getCacheCameraDir (Context context){
        return getCacheCameraDir(context,true);
    }

    /**
     * 相机存储路径优先SD卡存储
     * @param context
     * @param preferExternal
     * @return
     */
    public static File getCacheCameraDir(Context context,boolean preferExternal){
        File appCacheDir = null;
        if (preferExternal){
            appCacheDir = getExternalCameraFileDir(context);
        }
        if (appCacheDir == null) {
            appCacheDir = context.getCacheDir();
        }
        if (appCacheDir == null) {
            String cacheDirPath = "/data/data/" + context.getPackageName() + "/files/LeJu/";
            Logger.e("Can't define system cache directory! '%s' will be used.");
            appCacheDir = new File(cacheDirPath);
        }
        return appCacheDir;
    }

    /**
     * /storage/emulated/../Android/data/package_name/files/LeJu/
     * @param context
     * @return
     */
    private static File getExternalCameraFileDir(Context context){
        File externalCameraFileDir = getExternalCacheFileDir(context,false);
        if (externalCameraFileDir != null){
            File cameraFileDir = new File(externalCameraFileDir,"LeJu");
            if (!cameraFileDir.exists()){
                if (!cameraFileDir.mkdirs()){
                    System.out.println(" unable to create external cache directory ");
                    return null;
                }
            }
            return cameraFileDir;
        }
        return externalCameraFileDir;

    }

    private static boolean hasExternalStoragePermission(Context context){
        int permission = context.checkCallingOrSelfPermission(EXTERNAL_STORAGE_PERMISSION);
        return permission == PackageManager.PERMISSION_GRANTED;
    }


}
